package priv.mill.generator;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.yaml.snakeyaml.Yaml;

import freemarker.template.TemplateException;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import priv.mill.db.DBUtils;
import priv.mill.db.NameUtils;
import priv.mill.generator.util.FreemarkerUtil;

public class GenEntry {
	
	private static Logger logger = Logger.getLogger(GenEntry.class);
	public static void main(String[] args) throws SQLException, Exception {
		generatorAll();
		
	}

	private static void generatorAll() throws SQLException, Exception, IOException, TemplateException {
		List<Table> tables = getTables();
		genForm(tables);
		genEntry(tables);
	}

	private static void genForm(List<Table> tables) throws IOException, TemplateException {
		Yaml yaml=new Yaml();
		Map<String,Object> load = yaml.load(GenEntry.class.getClassLoader().getResourceAsStream("form.yml"));
		for (Table table : tables) {
			Map<String, Object> map = new HashMap<>();
			map.put("table", table);
			map.put("conf", load);
			map.put("under_camel", new TemplateMethodModelEx() {
				@Override
				public Object exec(List arguments) throws TemplateModelException {
					return NameUtils.under2Camel(arguments.get(0).toString());
				}
			});
			map.put("under_pasic", new TemplateMethodModelEx() {
				@Override
				public Object exec(List arguments) throws TemplateModelException {
					return NameUtils.under2Pasic(arguments.get(0).toString());
				}
			});
			File dist = new File(Config.INSTANCE.getHtml().getDisPath()+"/"+table.getTableName()+".html");
			logger.info("生成html:"+dist.getAbsolutePath());
			FreemarkerUtil.instance().toFile("html_form.ftl", map,dist);
		}
	}

	private static void genEntry(List<Table> tables) throws IOException, TemplateException {
		logger.info("开始生成实体类");
		for (Table table : tables) {
			generatEntry(table);
			logger.info(table+"  over");
		}
	}

	private static void generatEntry(Table table) throws IOException, TemplateException {
		Map<String, Object> map = new HashMap<>();
		map.put("table", table);
		map.put("package", Config.INSTANCE.getEntry().getPkg());
		map.put("under_camel", new TemplateMethodModelEx() {
			@Override
			public Object exec(List arguments) throws TemplateModelException {
				return NameUtils.under2Camel(arguments.get(0).toString());
			}
		});
		map.put("under_pasic", new TemplateMethodModelEx() {
			@Override
			public Object exec(List arguments) throws TemplateModelException {
				return NameUtils.under2Pasic(arguments.get(0).toString());
			}
		});
//		File file = new File(Config.INSTANCE.getEntry().getDistPath());
		File dist = new File(Config.INSTANCE.getEntry().getDistPath()+"/"
				+NameUtils.under2Pasic(table.getTableName())+".java");
		logger.info("生成:"+dist.getAbsolutePath());
		FreemarkerUtil.instance().toFile("Entry.ftl", map, dist);
	}

	private static List<Table> getTables() throws SQLException, Exception {
		logger.info("查询所有的表名");
		List<Table> tables = DBUtils.query(Table.class,"show tables");
		logger.info("添加列信息");
		for (Table table : tables) {
			List<Column> columns = DBUtils.query(Column.class,"show full columns from "+table.getTableName());
			table.setColumns(columns);
//			break;
		}
		logger.info("数据库表信息获取完毕");
		return tables;
	}
}
